#
# Module Name:
#    makefile
#
# Abstract:
#    Make file for floppy boot sector and loader.  It's FAT12 format.
#
#  Author:
#    Josh Wu
#    rainyfeeling@hotmail.com
#    09.20.2008
# 
#  NOTE:
#    target: prerequisites
#       $@ means first word of target.
#       $< means first word of prerequisites.

BIN_FILES_PATH = ./bin/

ASM = gcc
ASM_FLAGS = -c
BOOT_ENTRY_POINT = 0x7c00
LOADER_ENTRY_POINT = 0x100
LINKER = ld
BOOT_LINKER_FLAGS = --section-alignment 256 --file-alignment 256 -s -e c -Ttext $(BOOT_ENTRY_POINT)
LOADER_LINKER_FLAGS = --section-alignment 256 --file-alignment 256 -s -e c -Ttext $(LOADER_ENTRY_POINT)

BOOT_BIN_FILE = $(BIN_FILES_PATH)bootsector.bin
BOOT_ELF_FILE = $(BIN_FILES_PATH)bootsector.elf
BOOT_OBJ_FILE = $(BIN_FILES_PATH)bootsector.o
BOOT_SOURCE_FILE = bootsector.s fat12head.inc function16.s

LOADER_BIN_FILE = $(BIN_FILES_PATH)loader.bin
LOADER_ELF_FILE = $(BIN_FILES_PATH)loader.elf
LOADER_OBJ_FILE = $(BIN_FILES_PATH)loader.o
LOADER_SOURCE_FILE = loader.s fat12head.inc function16.s function32.s

BIN_FILES = $(BOOT_BIN_FILE) $(LOADER_BIN_FILE)

.PHONY : everything clean all

# Default starting position.
everything: $(BIN_FILES)

clean: 
	del .\bin\*.o
	del .\bin\*.elf
	del .\bin\*.bin
    
all: clean everything

$(BOOT_OBJ_FILE): $(BOOT_SOURCE_FILE)
	$(ASM) $(ASM_FLAGS) -o $@ $<
    
$(BOOT_BIN_FILE): $(BOOT_OBJ_FILE)
	$(LINKER) $(BOOT_LINKER_FLAGS) -o $(BOOT_ELF_FILE) $(BOOT_OBJ_FILE)
	objcopy -R .pdr -R .comment -R .note -S -O binary $(BOOT_ELF_FILE) $(BOOT_BIN_FILE)
	copy bin\bootsector.bin c:\temp
    
$(LOADER_OBJ_FILE): $(LOADER_SOURCE_FILE)
	$(ASM) $(ASM_FLAGS) -o $@ $<
    
$(LOADER_BIN_FILE): $(LOADER_OBJ_FILE)
	$(LINKER) $(LOADER_LINKER_FLAGS) -o $(LOADER_ELF_FILE) $(LOADER_OBJ_FILE)
	objcopy -R .pdr -R .comment -R .note -S -O binary $(LOADER_ELF_FILE) $(LOADER_BIN_FILE)
	copy bin\loader.bin c:\temp
    
    
    
    
